﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using GameView.Comment;
using GameView.Models;
using GameView.Services;
using GameView.Services.Comment;
using GameView.Services.Dtos;
using GameView.Services.Dtos.Project;
using GameView.Services.Dtos.WithdrawalDto;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Xml.Linq;

namespace GameView.ViewModels
{
   
    public partial class RoleViewModel : ObservableObject
    {
        private readonly RoleService _service;
        private readonly MenuService _menuService;

        public RoleViewModel(RoleService service, MenuService proService)
        {
            _menuService = proService;
            _service = service;
            LoadData().ConfigureAwait(false);
        }
        private List<MenuEntity> pgs = [];
        [ObservableProperty]
        private bool _isLoading = false;
        [ObservableProperty]
        private ObservableCollection<RoleOutDto> _roles;
        [ObservableProperty]
        private ObservableCollection<MenuEntity> _menuOptions = [];
        [ObservableProperty]
        [NotifyPropertyChangedFor(nameof(MenuOptions))] // 添加此特性
        private string _selectedRole;
        //[RelayCommand]
        //private async Task AddNewAsync()
        //{
        //    await App.ServiceProvider.GetRequiredService<IDialogService>()
        //  .ShowDialog<WithdrawalUpdateView>();
        //    await LoadFromApi();
        //}
        //[RelayCommand]
        //private async Task AddNewMAsync()
        //{
        //    await App.ServiceProvider.GetRequiredService<IDialogService>()
        //  .ShowDialog<WithdrawalUpdateView>();
        //    await LoadFromApi();
        //}

        partial void OnSelectedRoleChanged(string value)
        {
            // 清空游戏选项
           
        }
       
       
        [RelayCommand]
        private async Task LoadData()
        {
            IsLoading = true;
            await Task.Delay(200);
            try
            {
                await LoadFromApi();
                if (StaticValues.LoginInfo.PersonInfo.RoleId != 1)
                {
                    var res = await _proService.GetByPersonIdAsync();
                    pgs = res.Data;
                }
                else
                {
                    var res = await _proService.GetAllAsync();
                    pgs = res.Data;
                }

                ProjectOptions.Clear();
                GameOptions.Clear();
                foreach (var data in pgs)
                {
                    ProjectOptions.Add(data.Name);
                }
                SelectedProject = ProjectOptions[0];
            }
            catch (Exception ex)
            {
                GlobalMessage.Instance.ShowMessageDialog($"数据加载错误：{ex.Message}");
            }
            finally
            { IsLoading = false; }
        }
        private async Task LoadFromApi()
        {
            var validationResults = QueryDto.Validate(new ValidationContext(QueryDto)).ToList();
            QueryDto.PageIndex = PageIndex;
            QueryDto.PageSize = PageSize;
            if (!SelectedProject.IsNullOrEmpty())
                QueryDto.ProjectName = SelectedProject;
            var res = await _service.GetPageAsync(QueryDto);
            if (res.StatusCode != 200)
            {
                GlobalMessage.Instance.ShowMessageDialog(res.Errors);
                return;
            }
            PageSize = res.Data.PageSize;
            TotalPages = res.Data.TotalPages;
            TotalCount = res.Data.TotalCount;
            HasNextPages = res.Data.HasNextPages;
            HasPrevPages = res.Data.HasPrevPages;
            PageIndex = res.Data.PageIndex;
            RechargeRecords = new(res.Data.Items);

        }
    }
}
